package com.ruoyi.yyq.listener;

import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.yyq.domain.CarOrder;
import com.ruoyi.yyq.domain.CarRescueTask;
import com.ruoyi.yyq.service.ICarOrderService;
import com.ruoyi.yyq.service.ICarRescueTaskService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MQListener {
    @Autowired
    private ICarRescueTaskService carRescueTaskService;
    @Autowired
    private ICarOrderService carOrderService;

    @RabbitListener(queues = "order_dead_queue")
    public void orderMessage(CarOrder carOrder) {
        // 获取最新订单状态
        CarOrder latestOrder = carOrderService.selectCarOrderById(carOrder.getId());
        // 如果状态仍为0（未支付），则更新为3(已取消）
        if (latestOrder!= null && latestOrder.getStatus() == 0) {
            latestOrder.setStatus(3L);  // 3表示已取消
            carOrderService.updateCarOrder(latestOrder);
            System.out.println("订单取消处理完成，订单ID: " + latestOrder.getId());
        }
        System.out.println("订单消息为: " + carOrder);
    }
    //监听死信队列改变救援订单状态
    @RabbitListener(queues = "help_dead_queue")
    public void helpMessage(CarRescueTask carRescueTask) {
        // 获取最新订单状态
        CarRescueTask latestTask = carRescueTaskService.selectCarRescueTaskById(carRescueTask.getId());

        // 如果状态仍为0（求助中），则更新为2（超时）
        if (latestTask != null && latestTask.getStatus() == 0) {
            latestTask.setStatus(2L);  // 2表示超时
            carRescueTaskService.updateCarRescueTask(latestTask);
            System.out.println("救援订单超时处理完成，订单ID: " + latestTask.getId());
        }
        System.out.println("救援消息为: " + carRescueTask);
    }

}
